home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 24 / 6 / DISK2468.ZIP / MM / SAYERR.C < prev   
Text File  |  1990-03-24  |  5KB  |  169 lines

  1. /**************************************************************
  2. *        Popup error reporting package (sayerr.c)                *
  3. ***************************************************************/
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <conio.h>
  7. #include <bios.h>
  8. #include <string.h>
  9. #include <dos.h>
  10. #include <process.h>
  11. #include <errno.h>
  12. #include "popup.h"
  13. #include "\tc\myincs\mouse.h"
  14. static char *level_msg[] = {"WARNING","ERROR"};
  15. static char *pmsg = "Press <Enter> or <ESC> ...";
  16.  
  17. /*    The way the error/trace windows work:  If errw/msgw is NULL, then
  18.     an error/trace message is popped up at errx/msgx, erry/msgy.
  19.     Else, the window errw/msgw (which should have been already
  20.     popped up) will be used.  If errw/msgw is already defined, then
  21.     the errx/msgx and erry/msgy coordinates are ignored. */
  22.  
  23. int errx = CTRWIN;            /* default to center of the screen */
  24. int erry = CTRWIN;
  25. windesc *errw = NULL;        /* default to popup window */
  26. int msgx = CTRWIN;            /* same for messages */
  27. int msgy = CTRWIN;
  28. windesc *msgw = NULL;
  29.  
  30. void numnewlines(char *s, int *n, int *w)
  31. /*    Count number of newlines in the string s, also, return
  32.     maximum width of the lines of the message */
  33. {
  34.     int j,k;
  35.     for (*n=0,j=0,*w=0,k=0; (s[j] != 0); j++,k++) {
  36.         if (s[j] == '\n') {
  37.             (*n)++;
  38.             if (k > *w) *w = k; k = 0;
  39.         }
  40.         if (k > *w) *w = k;
  41.     }
  42. }
  43.  
  44. void popmsg(int x,int y,char *msg,char *title,char soundout,wincolors *wc)
  45. /*    Pops up the message msg at (x,y) - (absolute coordinates),
  46.     with title as the window header.  If soundout != 0, then the
  47.     alarm will sound.  It will then wait for a key press before
  48.     unpopping the window.
  49.     The message can contain newlines, which are accounted for in
  50.     the size of the window.  Note: it SHOULD usually contain a new
  51.     line at the end, or your message might scroll off the screen.
  52.     The (x,y) cursor positions use the same codes as for popup windows. */
  53. {
  54.     int wd, ht, c, k;
  55.     windesc *w;
  56.     /* Compute height and width of iwndow */
  57.     numnewlines(msg,&ht,&wd);
  58.     /* add in forder, and prompt message to size computations */
  59.     ht += 3;
  60.     if (wd < (strlen(pmsg)+1)) wd = strlen(pmsg)+3; else wd += 3;
  61.     w = draw_win(x, y, wd, ht, title, popup, wc);
  62.     mouse_off(1);
  63.     cprintf(msg);
  64.     cprintf(pmsg);
  65.     mouse_on(1);
  66.     if (soundout) beep();
  67.     do {
  68.         while(!(k = mouse_trigger(0)));
  69.         if (k == CTRLC) exit(0);                    /* control c aborts */
  70.         if (k != CRKEY && k != ESCKEY) beep();    /* only cr or esc allowed */
  71.     } while (k != CRKEY && k != ESCKEY);
  72.     rmv_win(w);
  73. }
  74.  
  75. void reperr(int level, char *msg)
  76. /* Reports the error in the error window.
  77.     Level = 0 means warning, level = 1 means error. */
  78. {
  79.     windesc *wsave;
  80.     if (errw == NULL) {    /*popup error window */
  81.         popmsg(errx, erry, msg, level_msg[level], 1, &errcolors);
  82.     }
  83.     else {    /* else route to existing error window */
  84.         wsave = curr_win;
  85.         slct_win(errw);
  86.         mprintf("%s: %s", level_msg[level], msg);
  87.         slct_win(wsave);
  88.     }
  89. }
  90.  
  91. void repmsg(char *msg)
  92. /*    Reports the message in the message window. */
  93. {
  94.     windesc *wsave;
  95.     if (msgw == NULL) {    /* popup message window */
  96.         popmsg(msgx, msgy, msg," Msg ", 0, &msgcolors);
  97.     }
  98.     else {    /* else route to existing message window */
  99.         wsave = curr_win;
  100.         slct_win(msgw);
  101.         mprintf("Msg: &s", msg);
  102.         slct_win(wsave);
  103.     }
  104. }
  105.  
  106. void sayerr(int ferr, int errflag, int lno, char *pname, char *fmt,...)
  107. /*    The parameters work as follows:
  108.     ferr            if 1, then the last DOS error message is printed
  109.     errflag        if 1, treat as warning, 2 treat as error, else
  110.                     just treat as a message
  111.     lno,pname    if pname = "", they're ignored, else they're treated
  112.                     as a line number and source file name.
  113.     fmt,...        a format string followed by optional arguments
  114.                     NOTE: formatting should not exceed 255 characters */
  115. {
  116.     va_list arg_ptr;
  117.     char t[255];
  118.     int j;
  119.     if (*pname) {
  120.         j = sprintf(t, "On line %d in pgm %s\r\n", lno, pname);
  121.     }
  122.     else j = 0;
  123.     if (ferr == 1) {
  124.         j += sprintf(t+j, "%s\r", strerror(errno));    /* add last DOS error */
  125.     }
  126.     va_start(arg_ptr,fmt);            /* point to optional arguments */
  127.     vsprintf(t+j, fmt, arg_ptr);    /* add rest of formatted string */
  128.     va_end(arg_ptr);
  129.     switch(errflag) {
  130.         case 1:
  131.             reperr(0,t);        /* just a warning */
  132.             break;
  133.         case 2:
  134.             reperr(1,t);        /* an error */
  135.             break;
  136.         default:
  137.             repmsg(t);            /* or a plain old message */
  138.     }
  139. }
  140.  
  141. unsigned int getkey(void)
  142. /*    Waits for and returns the scan-ascii code of the next key available.
  143.     Does not echo.  Pops up an abort window on ctrl-c. */
  144. {
  145.     windesc *w;
  146.     int k;
  147.     while(1) {    /* loop until non-ctrl-c key, or abort */
  148.         k = bioskey(0);
  149.         if (k == CTRLC) {
  150.             w = draw_win(CTRWIN, CTRWIN, 25, 3, "", popup, &errcolors);
  151.             k = bioskey(0);
  152.             rmv_win(w);
  153.             if ((k == 0x1559) || (k == 0x1579)) {    /* "Y" or "y" */
  154.                 mouse_reset();    /* don't forget to reset the mouse */
  155.                 exit(1);
  156.             }
  157.         } else break;
  158.     }
  159.     return k;
  160. }
  161.  
  162. void beep(void)
  163. /* sounds the bell */
  164. {
  165.     sound(50);
  166.     delay(25);
  167.     nosound();
  168. }
  169.